package util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.io.FileUtils;

import play.Play;

public class ResourceOperate {
	/**
	 * 上传方法
	 * @param file	上传文件
	 * @param types 上传核对类型数组
	 * @return
	 */
	public static List upload(File file,String[] types){
		List list =  new ArrayList();
		boolean flag = false;
		String uploadPath = null;
		String endStr = splitStr(file.getName()).get(1).toString();  //拿到后缀
		//匹配文件类型
		boolean b = checkUploadType(endStr,types); 
		if(b){
			//日期文件夹
			Calendar ca = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			String dir = sdf.format(ca.getTime());
			uploadPath = "/public/upload/"+ dir+"/"+file.getName();
			File file2= Play.getFile(uploadPath);
			try {
				FileUtils.copyFile(file, file2);
			} catch (IOException e) {
				e.printStackTrace();
			} 
			flag = true ;
		}
		list.add(flag);
		list.add(uploadPath);
		return list;
	}
	
	
	/**
	 * 拆分字符串
	 * @param s 字符串
	 * @return 集合
	 */
	public static List splitStr(String s){
		List list = new ArrayList();
		int lastIndex = s.lastIndexOf(".");
		int length = s.length();
		StringBuffer sb = new StringBuffer(s);
		list.add(sb.delete(lastIndex, length).toString());  // 得到名字(前部分)
		sb.delete(0, length);
		sb.append(s);
		list.add(sb.delete(0,lastIndex+1).toString());   //得到后缀（后部分）
		return list;
	}
	
	/**核对上传类型
	 * @param type
	 */
	public static boolean checkUploadType(String type,String[] types ){
		for (String str : types) {
			if(str.equals(type)){
				return true;
			}
		}
		return false;
	}
	
	
	/**
	 * 设定上传文件规范
	 *//*
	public static String[] filterTypes( ) {
		String[] types = {"bmp","jpg","jpeg","png","gif","tif","zip"};  //jpg,gif,jpeg,png,bmp,tif,zip
		return types; 
	}*/
	
	
	/**
	 * 修改文件夹中的文件的名字
	 * @param path
	 * @param newName
	 */
	public static boolean reName(String path,String newName){ 
		boolean flag = false;
		String newpath = Play.applicationPath + path;  //如何获得文件绝对路径 ？？？--->Play.applicationPath 
		File f=new File(newpath);   
        String c = f.getParent();   
        File F=new File(c + File.separator +newName +"."+ splitStr(path).get(1) );
        if(f.renameTo(F)){  
        	flag = true ;
        } 
        System.out.println("flag = "+flag);
        return flag;
	}
	
	/**
	 * 删除指定目录下的文件
	 * @param picPath
	 * @return
	 */
	public static boolean deleteFile(String path) { 
		   boolean flag = false;   
		   File file = new File(path);   
		   	// 路径为文件且不为空则进行删除
		    if (file.isFile() && file.exists()) {   
		       file.delete();   
		        flag = true; 
		    }
		    return flag;   
		}  
	/*
	public static void main(String[] args) {
		String path ="D:/workspace/demm/public/upload/201204/06.gif" ;
		String path2 = Play.applicationPath+"/public/upload/201204/13.gif";
		System.out.println(path2);
		boolean b = new ResourceOperate().deleteFile(path);
		System.out.println(b);
	}*/
	
}
